#include "Timer.h"

void Timer_Init(void)
{
	timer_parameter_struct timer1_initpara; //定时器结构体 
	timer_parameter_struct timer2_initpara;
	rcu_periph_clock_enable(RCU_TIMER1); //开启定时器时钟
	rcu_periph_clock_enable(RCU_TIMER2); //打开定时器二
    
    rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);//配置定时器时钟，这个后面重点说
    
	timer_struct_para_init(&timer1_initpara);//将定时器结构体内参数配置成默认参数
	timer_struct_para_init(&timer2_initpara);
    timer_deinit(TIMER1); //复位定时器1
	timer_deinit(TIMER2); //复位定时器2

	//配置TIMER1
    timer1_initpara.prescaler         = 2400 - 1;//预分频
    timer1_initpara.alignedmode       = TIMER_COUNTER_EDGE; //边缘对齐
    timer1_initpara.counterdirection  = TIMER_COUNTER_UP; //向上计数方式
    timer1_initpara.period            = 1000 - 1; //计数值
    timer1_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer1_initpara.repetitioncounter = 0; //设置重复计数器值，0表示不重复计数，每次溢出都产生更新事件
    timer_init(TIMER1,&timer1_initpara);
	
	timer2_initpara.alignedmode 	  = TIMER_COUNTER_EDGE;		/* 对齐模式 */
	timer2_initpara.prescaler         = (24000 - 1);				/* 时钟预分频数 */
	timer2_initpara.clockdivision     = TIMER_CKDIV_DIV1;		/* 采样分频 */
	timer2_initpara.counterdirection  = TIMER_COUNTER_UP;		/* 向上计数模式 */
	timer2_initpara.period            = 20000;					/* 自动重载值 */
	timer_init(TIMER2,&timer2_initpara);
	
    timer_auto_reload_shadow_enable(TIMER1);//使能自动重加载
    timer_enable(TIMER1);//使能定时器1
	timer_enable(TIMER2);//使能定时器2

	nvic_irq_enable(TIMER2_IRQn, 1, 3);			    			/* TIMER2中断设置，抢占优先级0，子优先级3 */
	timer_interrupt_enable(TIMER2,TIMER_INT_UP);				/* 使能更新中断 */ 
}

//现在是两秒进一次
void TIMER2_IRQHandler(void)
{
    if(timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_UP)!=RESET)
	{
		timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP);
//		printf("cnt value = %u\n", cnt++);
    }
}
